﻿Imports System.Text
Imports System.Reflection
Imports System.IO

Public Class fEpub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim sText As String = cTools.sFileReader()

        If sText <> String.Empty Then

            Dim dStart As Decimal = sText.IndexOf("第九卷")
            Dim dEnd As Decimal = sText.IndexOf("第十卷")
            sText = sText.Substring(dStart, dEnd - dStart)
            Dim listNav As List(Of String) = cTools.listNav_SE(sText, "第九卷", vbCrLf, True)


            cEPUB.sBookName = "死人经 第九卷"
            cEPUB.sCreator = "冰临神下"
            cEPUB.sFileNB = "Si_Ren_Jing_09"
            cEPUB.StartEpub()
            Dim dNav As Decimal = 1

            For Each sNav As String In listNav

                Dim uChart As cFile = cEPUB.NewCharter(sNav.Replace("第九卷 刀在", "").Trim())
                Dim listChart As New List(Of String)

                If dNav = listNav.Count Then
                    listChart.Add(sText.Substring(sText.IndexOf(listNav(dNav - 1)) + listNav(dNav - 1).Length))
                Else
                    listChart = cTools.listNav_SE(sText, listNav(dNav - 1), listNav(dNav), False)
                End If

                If listChart.Count = 1 Then

                    Dim listLine As List(Of String) = listChart(0).Split(vbCrLf).ToList()

                    For Each ss As String In listLine
                        If Not ss.Trim() = String.Empty Then
                            uChart.sbText.Append(cEPUB.htmlAddText(ss.Trim()))
                        End If
                    Next
                End If
                uChart.sbText.Append(cEPUB.htmlBodyEnd)
                System.IO.File.WriteAllText(uChart.sPath, uChart.sbText.ToString(), Encoding.GetEncoding("utf-8"))
                dNav = dNav + 1
            Next

            cEPUB.EndEpub()


        End If

    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click


        cEPUB.sBookName = "オレたち花のバブル組"
        cEPUB.sCreator = "池井戸潤"
        cEPUB.sFileNB = "hzw02"
        cEPUB.ENC = cEPUB.encShift_JIS
        cEPUB.sLang = "ja"
        cEPUB.StartEpub()

        Dim srNav As New StreamReader("C:\Users\vmware\Desktop\AA\OEBPS\nav.xhtml", cEPUB.encUTF8)
        Dim sNav As String = srNav.ReadToEnd()
        srNav.Close()
        Dim listNar As List(Of String) = cTools.listNav_SE(sNav, "<li>", "</li>", False)
        listNar.RemoveAt(0)
        listNar.RemoveAt(0)

        Dim listnar_NEW As New List(Of String)
        For Each unitNar In listNar
            listnar_NEW.Add(unitNar.Substring(38, unitNar.Length - 39 - 3))
        Next

      
        For iCrt As Integer = 6 To 15
            Dim sPath As String = "C:\Users\vmware\Desktop\AA\OEBPS\story-00" & iCrt.ToString().PadLeft(2, "0") & ".xhtml"
            Dim sr As New StreamReader(sPath, cEPUB.encUTF8)
            Dim sFileReader As String = sr.ReadToEnd()
            sr.Close()

            Dim uChart As cFile = cEPUB.NewCharter(listnar_NEW(iCrt - 6).Trim())
            Dim listChart As List(Of String) = cTools.listNav_SE(sFileReader, "<p class=""p67"">", "</p>", False)

            For Each ss As String In listChart
                If Not ss.Trim() = String.Empty AndAlso ss.Trim() <> "<br/>" Then
                    uChart.sbText.Append(cEPUB.htmlAddText(ss.Trim()))
                End If
            Next

            uChart.sbText.Append(cEPUB.htmlBodyEnd)

            System.IO.File.WriteAllText(uChart.sPath, uChart.sbText.ToString(), Encoding.GetEncoding("utf-8"))

        Next

        cEPUB.EndEpub()


    End Sub










    Private Sub btnDebug_Click(sender As System.Object, e As System.EventArgs) Handles btnDebug.Click

        Dim lTimeZone As New List(Of Decimal)

        lTimeZone.Add(1000)
        lTimeZone.Add(1200)
        lTimeZone.Add(1800)
        lTimeZone.Add(2400)
        lTimeZone.Add(3400)


        Dim list As List(Of Decimal) = GetTimeZone(lTimeZone, 1000, 2500, 1100, 1130, 1300, 1330)




    End Sub




    Public Shared Function GetTimeZone(lTimeZone As List(Of Decimal),
                                       dInTime As Decimal, dOutTime As Decimal,
                                       dInRest1 As Decimal, dOutRest1 As Decimal,
                                       dInRest2 As Decimal, dOutRest2 As Decimal) As List(Of Decimal)

        GetTimeZone = New List(Of Decimal)
        Dim InTime As DateTime = ctDatetime(dInTime)
        Dim OutTime As DateTime = ctDatetime(dOutTime)
        Dim InRest1 As DateTime = ctDatetime(dInRest1)
        Dim OutRest1 As DateTime = ctDatetime(dOutRest1)
        Dim InRest2 As DateTime = ctDatetime(dInRest2)
        Dim OutRest2 As DateTime = ctDatetime(dOutRest2)

        For i As Integer = 0 To 3

            Dim ZoneStart As DateTime = ctDatetime(lTimeZone(i))
            Dim ZoneEnd As DateTime = ctDatetime(lTimeZone(i + 1))


            Dim dttWK_Start As DateTime = InTime
            If dttWK_Start < ZoneStart Then
                dttWK_Start = ZoneStart
            End If

            Dim dttWK_End As DateTime = OutTime
            If dttWK_End > ZoneEnd Then
                dttWK_End = ZoneEnd
            End If

            Dim dttRest_Start1 As DateTime = InRest1
            If dttRest_Start1 < ZoneStart Then
                dttRest_Start1 = ZoneStart
            End If

            Dim dttRest_End1 As DateTime = OutRest1
            If dttRest_End1 > ZoneEnd Then
                dttRest_End1 = ZoneEnd
            End If


            Dim dttRest_Start2 As DateTime = InRest2
            If dttRest_Start2 < ZoneStart Then
                dttRest_Start2 = ZoneStart
            End If

            Dim dttRest_End2 As DateTime = OutRest2
            If dttRest_End2 > ZoneEnd Then
                dttRest_End2 = ZoneEnd
            End If

            Dim tsWK As TimeSpan = dttWK_End.Subtract(dttWK_Start)
            Dim tsRest1 As TimeSpan = dttRest_End1.Subtract(dttRest_Start1)
            Dim tsRest2 As TimeSpan = dttRest_End2.Subtract(dttRest_Start2)

            Dim misWK As Decimal = tsWK.TotalMinutes
            Dim misRest1 As Decimal = tsRest1.TotalMinutes
            Dim misRest2 As Decimal = tsRest2.TotalMinutes

            If misWK < 0 Then
                misWK = 0
            End If
            If misRest1 < 0 Then
                misRest1 = 0
            End If
            If misRest2 < 0 Then
                misRest2 = 0
            End If


            Dim misWK4 As Decimal = 0
            If i = 3 Then

                Dim tsWK4 As TimeSpan = ctDatetime(lTimeZone(0)).Subtract(InTime)

                misWK4 = tsWK4.TotalMinutes

                If misWK4 < 0 Then
                    misWK4 = 0
                End If

            End If


            GetTimeZone.Add(misWK - misRest1 - misRest2 + misWK4)

        Next

    End Function


    Public Shared Function ctDatetime(ByVal dTime As Decimal) As DateTime

        Dim addDays As Integer = 0

        If dTime >= 2400 Then
            addDays = 1
            dTime = dTime - 2400
        End If

        ctDatetime = DateTime.ParseExact(dTime.ToString().PadLeft(4, "0"), "HHmm", Nothing)
        ctDatetime = ctDatetime.AddDays(addDays)


    End Function












End Class